/* Copyright (c) CERN
 * * Artistic License 2.0
 * */

import java.util.*;
import java.io.*;

/**
 * This class sorts stack distances in ascending order.
 *
 * Usage: java Sort input_file [output_file]
 *
 * @author Xavier Grehant
 */
class Sort {

   static double number; // number of samples

   static void write(int ground[], String filename) throws IOException {
      FileWriter outfile = new FileWriter(new File(filename));
      for (int i = ground.length - 1; i >= 0; i--) {
         outfile.write(ground[i] + "\n");
      }
      outfile.close();
   }

    public static void main(String args[]) {
        if (args.length >= 1) {
            try {
                // Open file
                FileInputStream fstream = new FileInputStream(args[0]);
                BufferedReader in = new BufferedReader(new InputStreamReader(fstream));
                Vector fromFile = new Vector();

                // Calculate useful stuff

                // number of samples
                String newValue;
                while ((newValue = in.readLine()) != null) {
                    Integer intValue = new Integer(newValue);
                    fromFile.add(intValue);
                }
                number = fromFile.size();
                int[] fromVector = new int[(int) number];
                for (int index = 0; index < number; index++) {
                   fromVector[index] = ((Integer) fromFile.elementAt(index)).intValue();
                }

                // Close file
                in.close();
                Arrays.sort(fromVector);
                String outfilename;
                if (args.length > 1) outfilename = args[1];
                else outfilename = "sort.out";
                write(fromVector, outfilename);
            } catch (IOException e) {
               e.printStackTrace();
            }
        }
        else
            System.out.println("Invalid parameters");
    }
}
